package com.itheima.filter;


import com.alibaba.fastjson.JSON;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.AntPathMatcher;


import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter(filterName = "LoginCheckFilter",urlPatterns = "/*")
@Slf4j
public class LoginCheckFilter implements Filter {
    public static final AntPathMatcher PATH_MATCHER = new AntPathMatcher();
    public static boolean isE=false;
    public static boolean isU=false;
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
            HttpServletRequest request=(HttpServletRequest) servletRequest;
            HttpServletResponse response=(HttpServletResponse) servletResponse;
        String requestURI = request.getRequestURI();
        log.info(requestURI);
            String[] arr={
                    "/employee/login",
                    "/employee/logout",
                    "/backend/**",
                    "/front/**",
                    "/common/*",
                    "/user/login",
                    "/user/sendMsg"
            };
            String[] employees={"/employee/page"};
            String[] users={"/shoppingCart/list"};
        boolean b1 = loginCheck(employees, requestURI);
        boolean b2 = loginCheck(users, requestURI);

        if (b1) {
            isE=true;
            isU=false;
        }

        if (b2) {
            isE=false;
            isU=true;
        }

        boolean result = loginCheck(arr, requestURI);
        if(result){
            filterChain.doFilter(request,response);
            log.info("拦截到 {} 放行",requestURI);
            return;
        }
        Long id1 = (Long) request.getSession().getAttribute("employee");
        Long id2 = (Long) request.getSession().getAttribute("user");



        if (id1!=null&&isE){
            BaseContext.setId(id1);
            Long id = BaseContext.getId();
            log.info("获取id为：{}",id);
            long idT = Thread.currentThread().getId();
            log.info("线程id为：{}",idT);
            filterChain.doFilter(request,response);
            log.info("用户已登录,用户id为{}",id);
            return;
        }

       if (id2!=null&&isU){
           BaseContext.setId(id2);
           Long id = BaseContext.getId();
           log.info("获取id为：{}",id);
           long idT = Thread.currentThread().getId();
           log.info("线程id为：{}",idT);
           filterChain.doFilter(request,response);
           log.info("用户已登录,用户id为{}",id);
           return;
       }


        log.info("用户未登录");
        //5、如果未登录则返回未登录结果，通过输出流方式向客户端页面响应数据
        response.getWriter().write(JSON.toJSONString(R.error("NOTLOGIN")));
        return;

    }
    public boolean loginCheck(String[] arr,String requestURI){
        for (String s : arr) {
            boolean match = PATH_MATCHER.match(s, requestURI);
            if(match){
                return true;
            }
        }
        return false;
    }
}
